//////////////////////////////////////////////////////////////////////////////////
//	 Copyright 2011 by Yosef Grabivker, ryuho@homenetworksoft.com
//
//   Licensed under the Apache License, Version 2.0 (the "License");
//   you may not use this file except in compliance with the License.
//   You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
//   Unless required by applicable law or agreed to in writing, software
//   distributed under the License is distributed on an "AS IS" BASIS,
//   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
//   See the License for the specific language governing permissions and
//   limitations under the License.
//////////////////////////////////////////////////////////////////////////////////

#include <wx/wx.h>
#include <wx/dcmemory.h>
#include "mainwnd.h"
#include "taskbaricon.h"
#include "guilogic.h"

#define TASK_BAR_ICON_W			16
#define TASK_BAR_ICON_H			16
#define TASK_BAR_ICON_BORDER_ST	2

enum cMenuIDs { ID_MENU_CLOSE=10000, ID_MENU_SHOW};

BEGIN_EVENT_TABLE(CTaskBarIcon, wxTaskBarIcon)
	EVT_TASKBAR_LEFT_DCLICK( CTaskBarIcon::OnDblClick )
	EVT_MENU(ID_MENU_CLOSE, CTaskBarIcon::OnClose)
	EVT_MENU(ID_MENU_SHOW, CTaskBarIcon::OnShow )
	EVT_MENU(wxID_REFRESH, CTaskBarIcon::OnRefresh )
END_EVENT_TABLE()


CTaskBarIcon::CTaskBarIcon(CMainWnd& wnd):m_wnd(wnd)
{
	//m_icon.SetWidth(TASK_BAR_ICON_W);
	//m_icon.SetHeight(TASK_BAR_ICON_H);
}

CTaskBarIcon::~CTaskBarIcon(void)
{
	CleanUp();
}

void CTaskBarIcon::CleanUp()
{
	if ( IsIconInstalled() )
	{
		RemoveIcon();
	}
}

void CTaskBarIcon::SetIcon(GUIBIND& bind, const wxString& strTip)
{
#if wxABI_VERSION>=20900
	if ( wxTaskBarIcon::IsAvailable() )
#endif
	{
		BuildIcon(bind);

		wxTaskBarIcon::SetIcon(m_icon, strTip );
	}
}

void CTaskBarIcon::BuildIcon(GUIBIND& bind)
{
	wxBitmap bmp(TASK_BAR_ICON_W, TASK_BAR_ICON_H);
	wxMemoryDC dc;
	
	unsigned short  bits[TASK_BAR_ICON_H];
	for( int i=0; i<TASK_BAR_ICON_H; i++ )
	{
		bits[i] = 0xc003;
	}

	//create monochrome bmp
	wxBitmap bmpMask((const char *)bits, TASK_BAR_ICON_W, TASK_BAR_ICON_H,1);

	wxMask* pMask = new wxMask(bmpMask);
	bmp.SetMask(pMask);

	dc.SelectObject(bmp);

	dc.SetBackgroundMode(wxTRANSPARENT); 
	dc.SetBackground( *wxTRANSPARENT_BRUSH ); 
	dc.Clear();

	DrawGrid(dc);
	DrawUsage(dc, bind);
	DrawBorder(dc);

	dc.SelectObject(wxNullBitmap);

	m_icon.CopyFromBitmap( bmp );
}

void CTaskBarIcon::DrawGrid(wxMemoryDC& dc)
{
	wxColour 	green(0,128,0);
	wxPen		pen(green);
	dc.SetPen(pen);

	for (int i=0;i<TASK_BAR_ICON_W;i+=2)
	{
		dc.DrawLine(i, 0, i, TASK_BAR_ICON_H );
	}

	for (int j=0;j<TASK_BAR_ICON_H;j+=2)
	{
		dc.DrawLine(0, j, TASK_BAR_ICON_W,j );
	}
}

void CTaskBarIcon::DrawUsage(wxMemoryDC& dc, GUIBIND& bind)
{
	wxColour 	green(0,0xFF,0);
	wxColour	yellow(255,255,0);
	wxColour	red(255,0,0);
	wxPen		pen(green);
	wxPen		penY(yellow);
	wxPen		penR(red);
	dc.SetPen(pen);

	unsigned long lFilled = 0;

	unsigned long lMax = bind.GetMaxVal();
	unsigned long lCur = bind.GetVal();

	if ( lMax )
	{
		double fUsage = ((double)(lCur))/lMax;
		lFilled = (unsigned long)(fUsage*TASK_BAR_ICON_H);

		if (lCur && !lFilled) lFilled=1;
	}

	for (unsigned int i=0;i<lFilled;i++)
	{
		unsigned char p = (i*100)/TASK_BAR_ICON_H;

		if ( CGUILogic::IsWarningLevel( bind, p ) )
		{
			dc.SetPen(penY);

		} else if ( CGUILogic::IsAlarmLevel( bind, p ) )
		{
			dc.SetPen(penR);
		}

		dc.DrawLine(0,TASK_BAR_ICON_H-i, TASK_BAR_ICON_W, TASK_BAR_ICON_H-i);
	}
}

void CTaskBarIcon::DrawBorder(class wxMemoryDC& dc)
{
	wxColour 	grayLt(228,228,228);
	wxColour 	gray(127,127,127);
	wxPen		grltPen(grayLt);
	wxPen		grPen(gray);
	dc.SetPen(grPen);

	dc.DrawLine(TASK_BAR_ICON_BORDER_ST, 0, TASK_BAR_ICON_W, 0 );
	dc.DrawLine(TASK_BAR_ICON_BORDER_ST, 0, TASK_BAR_ICON_BORDER_ST, TASK_BAR_ICON_H );

	dc.SetPen(grltPen);

	dc.DrawLine(TASK_BAR_ICON_BORDER_ST, TASK_BAR_ICON_H-1, TASK_BAR_ICON_W, TASK_BAR_ICON_H-1 );
	dc.DrawLine(TASK_BAR_ICON_W-TASK_BAR_ICON_BORDER_ST-1, 0, TASK_BAR_ICON_W-TASK_BAR_ICON_BORDER_ST-1, TASK_BAR_ICON_H );
}

wxMenu* CTaskBarIcon::CreatePopupMenu()
{
	wxMenu* pMenu = new wxMenu();

	pMenu->Append(ID_MENU_SHOW,  _("Show") );
	pMenu->Append(ID_MENU_CLOSE, _("Close") );
	pMenu->AppendSeparator();
	pMenu->Append(wxID_REFRESH, _("Refresh Now"), _("Refresh Now") );
	

	return pMenu;
}

void CTaskBarIcon::ShowMainWnd()
{
	m_wnd.Show(true);
	m_wnd.Restore();
	m_wnd.Raise();
}

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void CTaskBarIcon::OnDblClick(wxTaskBarIconEvent& event)
{
	ShowMainWnd();
}

void CTaskBarIcon::OnShow(wxCommandEvent& event)
{
	ShowMainWnd();
}

void CTaskBarIcon::OnClose(wxCommandEvent& event)
{
	m_wnd.Close();
}

void CTaskBarIcon::OnRefresh(wxCommandEvent& event)
{
	m_wnd.Refresh();
}

